<!DOCTYPE html>
<html lang="en">
<head>
    <!--编码格式-->
    <meta charset="UTF-8">
    <!--强制ie使用最新的edge引擎，chrome使用最新版本-->
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <!--双核模拟器，采用webkit内核-->
    <meta name="renderer" content="webkit"/>
    <!--移动端视窗-->
    <meta name="viewport"
          content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <!--页面描述-->
    <meta name="Description" content=""/>
    <!--关键字-->
    <meta name="Keywords" content=""/>
    <title>示例代码</title>
</head>
<body>
<div id="one" style="width: 100px;height: 100px;background: red"></div>
<script>

    var ws = new WebSocket("ws://localhost:8181");

    //建立连接时 触发的事件
    ws.onopen = function (e) {
        console.log('Connection to server opened');
    }

    //连接关闭时 触发的事件
    ws.onclose = function(e){
        console.log("close");
    }

    //收到服务器消息的时候 触发的事件
    ws.onmessage = function(e){
        console.log(e.data);
    }

    function sendMessage(user,msg) {
        //像服务器发送消息
        console.log("我:" + msg);
        ws.send(JSON.stringify({
            user:user,
            msg:msg
        }));
    }
    var one = document.getElementById("one")
    one.onclick = function(){
        sendMessage('haha','heiehi');
    }
</script>
</body>
</html>